********************************************************************************
* Preamble, globals, and import
********************************************************************************
clear all

* Change to proper import and export paths on your machine
global import "{path}/data"
global export "{path}/figs"

import delimited "${import}/conjoint_cn.csv"

********************************************************************************
* Define categories: joint development
********************************************************************************
* Create categories
gen category = .
	replace category = 1 if diaoyusenkakusovereignty == "China sovereignty" & chinatariff == "Increase 10%" & diaoyusenkakudev == "Joint development" 
	replace category = 2 if diaoyusenkakusovereignty == "China sovereignty" & chinatariff == "Increase 20%" & diaoyusenkakudev == "Joint development" 
	replace category = 3 if diaoyusenkakusovereignty == "China sovereignty" & chinatariff == "Decrease 10%" & diaoyusenkakudev == "Joint development" 
	replace category = 4 if diaoyusenkakusovereignty == "China sovereignty" & chinatariff == "Decrease 20%" & diaoyusenkakudev == "Joint development" 
	replace category = 5 if diaoyusenkakusovereignty == "China sovereignty" & japantariff == "Increase 10%" & diaoyusenkakudev == "Joint development" 
	replace category = 6 if diaoyusenkakusovereignty == "China sovereignty" & japantariff == "Increase 20%" & diaoyusenkakudev == "Joint development" 
	replace category = 7 if diaoyusenkakusovereignty == "China sovereignty" & japantariff == "Decrease 10%" & diaoyusenkakudev == "Joint development" 
	replace category = 8 if diaoyusenkakusovereignty == "China sovereignty" & japantariff == "Decrease 20%" & diaoyusenkakudev == "Joint development" 
	
	replace category = 9 if diaoyusenkakusovereignty == "Japan sovereignty" & chinatariff == "Increase 10%" & diaoyusenkakudev == "Joint development" 
	replace category = 10 if diaoyusenkakusovereignty == "Japan sovereignty" & chinatariff == "Increase 20%" & diaoyusenkakudev == "Joint development" 
	replace category = 11 if diaoyusenkakusovereignty == "Japan sovereignty" & chinatariff == "Decrease 10%" & diaoyusenkakudev == "Joint development" 
	replace category = 12 if diaoyusenkakusovereignty == "Japan sovereignty" & chinatariff == "Decrease 20%" & diaoyusenkakudev == "Joint development" 
	replace category = 13 if diaoyusenkakusovereignty == "Japan sovereignty" & japantariff == "Increase 10%" & diaoyusenkakudev == "Joint development" 
	replace category = 14 if diaoyusenkakusovereignty == "Japan sovereignty" & japantariff == "Increase 20%" & diaoyusenkakudev == "Joint development" 
	replace category = 15 if diaoyusenkakusovereignty == "Japan sovereignty" & japantariff == "Decrease 10%" & diaoyusenkakudev == "Joint development" 
	replace category = 16 if diaoyusenkakusovereignty == "Japan sovereignty" & japantariff == "Decrease 20%" & diaoyusenkakudev == "Joint development" 

* Name categories
label define category ////
		1 `" "China" "+10%" "' ////
		2 `" "China" "+20%" "' ////
		3 `" "China" "-10%" "' ////
		4 `" "China" "-20%" "' ////
		5 `" "Japan" "+10%" "' ////
		6 `" "Japan" "+20%" "' ////
		7 `" "Japan" "-10%" "' ////
		8 `" "Japan" "-20%" "' ////
		9 `" "China" "+10%" "' ////
		10 `" "China" "+20%" "' ////
		11 `" "China" "-10%" "' ////
		12 `" "China" "-20%" "' ////
		13 `" "Japan" "+10%" "' ////
		14 `" "Japan" "+20%" "' ////
		15 `" "Japan" "-10%" "' ////
		16 `" "Japan" "-20%" "' ////

label val category category
	
********************************************************************************
* Calculate marginal effects
********************************************************************************	
reg chosen i.category, cl(id)
margins category	
		
********************************************************************************
* Plot marginal effects
********************************************************************************
* Sort results
matrix plot = r(table)'

matsort plot 1 "up"

matrix plot = plot'

* Create plot
coefplot (matrix(plot[1,])), ci((plot[5,] plot[6,])) ///
	vertical ///
	scheme(s1color) ///
	ylabel(0(.1)1) ///
	xlabel(, labsize(vsmall)) ///
	xline(8.5, lstyle(grid)) ///
	yline(.5, lstyle(grid))  ///
	ytitle("Probability of choosing deal (%)" " ") ///
	xtitle(" " "Trade policy") ///
	text(.9 3 "Japan sovereignty", place(e)) ///
	text(.9 11 "China sovereignty", place(e)) ///
	xsize(7) ysize(3.5) ///
	color(midblue) ciopts(lcolor(gs10))

* Export plot
graph export "${export}/A1. mm_sovereignty_china_jointdev.pdf", replace

********************************************************************************
* Define categories: sole development
********************************************************************************
* Create categories
replace category = .
	replace category = 1 if diaoyusenkakusovereignty == "China sovereignty" & chinatariff == "Increase 10%" & diaoyusenkakudev == "China development" 
	replace category = 2 if diaoyusenkakusovereignty == "China sovereignty" & chinatariff == "Increase 20%" & diaoyusenkakudev == "China development" 
	replace category = 3 if diaoyusenkakusovereignty == "China sovereignty" & chinatariff == "Decrease 10%" & diaoyusenkakudev == "China development" 
	replace category = 4 if diaoyusenkakusovereignty == "China sovereignty" & chinatariff == "Decrease 20%" & diaoyusenkakudev == "China development" 
	replace category = 5 if diaoyusenkakusovereignty == "China sovereignty" & japantariff == "Increase 10%" & diaoyusenkakudev == "China development" 
	replace category = 6 if diaoyusenkakusovereignty == "China sovereignty" & japantariff == "Increase 20%" & diaoyusenkakudev == "China development" 
	replace category = 7 if diaoyusenkakusovereignty == "China sovereignty" & japantariff == "Decrease 10%" & diaoyusenkakudev == "China development" 
	replace category = 8 if diaoyusenkakusovereignty == "China sovereignty" & japantariff == "Decrease 20%" & diaoyusenkakudev == "China development" 
	
	replace category = 9 if diaoyusenkakusovereignty == "Japan sovereignty" & chinatariff == "Increase 10%" & diaoyusenkakudev == "Japan development" 
	replace category = 10 if diaoyusenkakusovereignty == "Japan sovereignty" & chinatariff == "Increase 20%" & diaoyusenkakudev == "Japan development" 
	replace category = 11 if diaoyusenkakusovereignty == "Japan sovereignty" & chinatariff == "Decrease 10%" & diaoyusenkakudev == "Japan development" 
	replace category = 12 if diaoyusenkakusovereignty == "Japan sovereignty" & chinatariff == "Decrease 20%" & diaoyusenkakudev == "Japan development" 
	replace category = 13 if diaoyusenkakusovereignty == "Japan sovereignty" & japantariff == "Increase 10%" & diaoyusenkakudev == "Japan development" 
	replace category = 14 if diaoyusenkakusovereignty == "Japan sovereignty" & japantariff == "Increase 20%" & diaoyusenkakudev == "Japan development" 
	replace category = 15 if diaoyusenkakusovereignty == "Japan sovereignty" & japantariff == "Decrease 10%" & diaoyusenkakudev == "Japan development" 
	replace category = 16 if diaoyusenkakusovereignty == "Japan sovereignty" & japantariff == "Decrease 20%" & diaoyusenkakudev == "Japan development" 

* Name categories
label drop category
label define category ////
		1 `" "China" "+10%" "' ////
		2 `" "China" "+20%" "' ////
		3 `" "China" "-10%" "' ////
		4 `" "China" "-20%" "' ////
		5 `" "Japan" "+10%" "' ////
		6 `" "Japan" "+20%" "' ////
		7 `" "Japan" "-10%" "' ////
		8 `" "Japan" "-20%" "' ////
		9 `" "China" "+10%" "' ////
		10 `" "China" "+20%" "' ////
		11 `" "China" "-10%" "' ////
		12 `" "China" "-20%" "' ////
		13 `" "Japan" "+10%" "' ////
		14 `" "Japan" "+20%" "' ////
		15 `" "Japan" "-10%" "' ////
		16 `" "Japan" "-20%" "' ////

label val category category
	
********************************************************************************
* Calculate marginal effects
********************************************************************************	
reg chosen i.category, cl(id)
margins category	
		
********************************************************************************
* Plot marginal effects
********************************************************************************
* Sort results
matrix plot = r(table)'

matsort plot 1 "up"

matrix plot = plot'

* Create plot
coefplot (matrix(plot[1,])), ci((plot[5,] plot[6,])) ///
	vertical ///
	scheme(s1color) ///
	ylabel(0(.1)1) ///
	xlabel(, labsize(vsmall)) ///
	xline(8.5, lstyle(grid)) ///
	yline(.5, lstyle(grid))  ///
	ytitle("Probability of choosing deal (%)" " ") ///
	xtitle(" " "Trade policy") ///
	text(.9 3 "Japan sovereignty", place(e)) ///
	text(.9 11 "China sovereignty", place(e)) ///
	xsize(7) ysize(3.5) ///
	color(midblue) ciopts(lcolor(gs10))

* Export plot
graph export "${export}/A2. mm_sovereignty_china_soledev.pdf", replace
